﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FazaDemo.BL.CM.CollectionMechanisms
{
    public static class EnumerableExtensions
    {

        public static IEnumerable<List<T>> Split<T>(this IEnumerable<T> enumerable, T separator)
        {
            var lists = new List<List<T>>();
            var enumList = enumerable as IList<T> ?? enumerable.ToList();
            int enumCount = enumList.Count();
            for (int i = 0; i < enumCount; i++)
            {
                lists.Add(enumList.SkipTakeWhile(e => e.Equals(separator), e => !e.Equals(separator))
                    .ToList());
                int lastIndex = lists.Count - 1;
                int skipCount = lists[lastIndex].Count + 1;
                enumList = enumList.SkipWhile((t, j) => j < skipCount)
                    .ToList();
                i += (skipCount - 1);
            }
            return lists;
        }


        public static IEnumerable<T> SkipTakeWhile<T>(this IEnumerable<T> enumerable, Func<T, bool> skipPredicate,
            Func<T, bool> takePredicate)
        {
            return enumerable.SkipWhile(skipPredicate).TakeWhile(takePredicate);
        }


        public static IEnumerable<T> SkipTakeWhile<T>(this IEnumerable<T> enumerable, Func<T, int, bool> skipPredicate,
            Func<T, bool> takePredicate)
        {
            return enumerable.SkipWhile(skipPredicate).TakeWhile(takePredicate);
        }

    }
}
